﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS.longACSS
{
    public partial class frmMidFinalTerm : Form
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();

        public frmMidFinalTerm()
        {
            InitializeComponent();
        }

        private void frmMidFinalTerm_Load(object sender, EventArgs e)
        {
            LoadControl();
        }

        private void btn_CapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_BoQua_Click(object sender, EventArgs e)
        {
            try
            {
                ds.RejectChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region "Coding"
        private void LoadControl()
        {
            LoadData();
        }

        private void LoadData()
        {
            try
            {
                LoadMidFinalTerm();
                LoadKhoaHoc();
                gridControl_Data.DataSource = ds.Tables["TableMidFinalTerm"];
                repositoryItemSearchLookUpEdit1.DataSource = ds.Tables["TableKhoaHoc"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadKhoaHoc()
        {
            try
            {
                if (ds.Tables["TableKhoaHoc"] != null)
                    ds.Tables["TableKhoaHoc"].Clear();
                string sql = @"select MaKhoaHoc, TenKhoaHoc from DM_KHOAHOC";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableKhoaHoc");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadMidFinalTerm()
        {
            if (ds.Tables["TableMidFinalTerm"] != null)
                ds.Tables["TableMidFinalTerm"].Clear();
            string sql = @"select * from MidTerm_FinalTerm";
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            da.Fill(ds, "TableMidFinalTerm");
        }

        private void UpdateData()
        {
            try
            {
                string sql = @"select * from MidTerm_FinalTerm";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataHelper.buildDataCommands(ref da);
                da.Update(ds, "TableMidFinalTerm");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
